package com.fingard.dsp.bank.directbank.srb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class SRB01Transaction extends SRB01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            Calendar qryFromCal = Calendar.getInstance();   //查询起始日期
            Calendar qryToCal = Calendar.getInstance();     //查询截止日期
            Calendar curFromCal = Calendar.getInstance();   //当前查询起始日期
            Calendar curToCal = Calendar.getInstance();     //当前查询截止日期

            //初始化参数
            curFromCal.setTime(p_req7516.getDateFrom());
            qryToCal.setTime(p_req7516.getDateTo());

            //按照自然月分月查询
            while(curFromCal.compareTo(qryToCal) <=0 && tmpRespCode.length() == 0){
                //计算本次查询截止日期
                if (DateHelper.compareMonth(curFromCal, qryToCal) == -1 ){
                    curToCal.setTime(curFromCal.getTime());
                    curToCal.add(Calendar.DAY_OF_MONTH, curFromCal.getActualMaximum(Calendar.DAY_OF_MONTH));
                } {
                    curToCal.setTime(qryToCal.getTime());
                }

                XmlTextWriter xmlWriter = new XmlTextWriter();
                SRB01Header bankHeader = new SRB01Header();
                bankHeader.trCode = "200110";//交易代码
                bankHeader.reqSeqNo = p_req7516.reqHeader.transSeqID;

                writeCommonHeader(xmlWriter, bankHeader, false);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("acno", p_req7516.actNum);
                xmlWriter.writeElementString("cur_code", convertCurToBank(p_req7516.actCur));
                xmlWriter.writeElementString("start_date", Format.Date8Format.format(curFromCal.getTime()));
                xmlWriter.writeElementString("end_date", Format.Date8Format.format(curToCal.getTime()));
                xmlWriter.writeEndElement();//body
                xmlWriter.writeEndElement();//ap
                xmlWriter.writeEndDocument();

                String[] retStrArr = sendToBank(xmlWriter.toXmlString());

                Map<String, String> parseResultMap = parseRespMsg(retStrArr, retAts, p_req7516.actArea, null);
                tmpRespCode = parseResultMap.get("tmpRespCode");
                tmpRespInfo = parseResultMap.get("tmpRespInfo");

                curFromCal.setTime(curToCal.getTime());
                curFromCal.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询明细异常:"+e);
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespInfo(tmpRespInfo);
            retAts.writeRespCode(tmpRespCode);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        tmpReq7516.higherLevelAct = p_req7515.higherLevelAct;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
